package com.alangyun.remind.service.impl;

import java.util.Arrays;

import org.springframework.stereotype.Service;

import com.alangyun.remind.BaseActor;
import com.alangyun.remind.error.MindException;
import com.alangyun.remind.repository.StoreEngine;
import com.alangyun.remind.repository.cache.LeaderCache;
import com.alangyun.remind.repository.data.LeaderCaution;
import com.alangyun.remind.repository.data.LeaderDataset;
import com.alangyun.remind.repository.data.NameTerm;
import com.alangyun.remind.service.ILeaderService;
import com.alangyun.remind.utils.Utility;
import com.alangyun.remind.vo.FilterParam;
import com.alangyun.remind.vo.leader.LeaderSum;
import com.alangyun.remind.vo.leader.LeaderUser;
import com.alangyun.remind.vo.leader.LeaderView;

@Service
public class LeaderService extends BaseActor implements ILeaderService {
	private static String[] SumTypes = new String[] {  
			"超出年限" ,
			"一年以内", 
			"1（含）~2年", 
			"2（含）~3年", 
			"3（含）~4年", 
			"4（含）~5年"
	};
	private static int normalCycle = 6;//轮岗年限
	private static int specialCycle = 5;//轮岗年限
	
//	private static int maleHide = 57;//退二线年限
	private static int maleTire =60;//退休年限

//	private static int femaleHide = 52;//退二线年限
	private static int femaleTire =55;//退休年限

	private boolean isTired(String sex, int age) {
		if (Utility.isMale(sex))
			return age >= maleTire;
		else
			return age >= femaleTire;
	}

//	private boolean isBehide(String sex, int age) {
//		if (Utility.isMale(sex))
//			return age >= maleHide;
//		else
//			return age >= femaleHide;
//	}

	private void validateDataset() {
		if (!LeaderCache.my().isLoaded()) {
			try {
				LeaderDataset data = StoreEngine.openDataset(LeaderDataset.class,
						new Class<?>[] { LeaderCaution.class, NameTerm.class });
				LeaderCache.my().addDataset(data);
			} catch (Exception ex) {
				throw new MindException("检查领导干部预警数据可用性失败，请确认是否已上传预警数据", ex);
			}
		}
	}

	@Override
	public LeaderView listLeaderRatio(Integer[] depart, Integer[] profs, Integer[] jobs) {

		validateDataset();

		LeaderDataset data = LeaderCache.my().get();
		if (data == null)
			return null;

		LeaderView ret = LeaderView.create();
		int[] counts = new int[SumTypes.length];
		Arrays.fill(counts, 0);
		int number = 0;
		for (LeaderCaution caution : data.getDatas()) {
//			int yold = caution.getSgsj()==null?0: Utility.ageLimit(caution.getSgsj());
			if (Utility.inArray(depart, caution.getDepart()) && Utility.inArray(profs, caution.getTechLevel())
					&& Utility.inArray(jobs, caution.getJob01())) {
				
				int maxDistance = caution.getLgnx() == null ? normalCycle : caution.getLgnx();
				int distance = (int) (caution.getJllgsj() == null ? 0 : caution.getJllgsj());
				if (distance <= 0 && caution.getSgsj()!=null) {
					distance=Utility.ageLimit(caution.getSgsj());
					distance= maxDistance-distance;
				}

				LeaderUser item = LeaderUser.from(caution);
				item.setLgnx(maxDistance);
				item.setJob(caution.getJob01Name());
				item.setJllgsj((double) distance);
				if (distance < 0)
					counts[0]++;
				else if (distance < 1)
					counts[1]++;
				else if (distance < 2)
					counts[2]++;
				else if (distance < 3)
					counts[3]++;
				else if (distance < 4)
					counts[4]++;
				else if (distance < 5)
					counts[5]++;

				number++;
				ret.getWorkers().add(item);
			}
		}

		for (int index = 0; index < SumTypes.length; index++) {
			ret.getSums().add(LeaderSum.create(SumTypes[index], counts[index], number));
		}

		return ret;
	}

	@Override
	public LeaderView listTiredRatio(Integer[] depart, Integer[] profs, Integer[] jobs) {

		validateDataset();

		LeaderDataset data = LeaderCache.my().get();
		if (data == null)
			return null;

		LeaderView ret = LeaderView.create();
		int[] counts = new int[SumTypes.length];
		Arrays.fill(counts, 0);
		int number = 0;
		int typeLen = SumTypes.length;
		for (LeaderCaution caution : data.getDatas()) {
			int yold = caution.getYearOld();
			if (Utility.inArray(depart, caution.getDepart()) && Utility.inArray(profs, caution.getTechLevel())
					&& Utility.inArray(jobs, caution.getJob01())) {

				int maxTireDistance = maleTire;
				if(!Utility.isMale(caution.getGender()))
					maxTireDistance = femaleTire;
				int distance = maxTireDistance- yold;

				LeaderUser item = LeaderUser.from(caution);
				item.setLgnx(maxTireDistance);
				item.setJob(caution.getJob01Name());
				item.setJllgsj((double) distance);
				if (distance < 0)
					counts[0]++;
				else if (distance < 1)
					counts[1]++;
				else if (distance < 2)
					counts[2]++;
				else if (distance < 3)
					counts[3]++;
				else if (distance < 4)
					counts[4]++;
				else if (distance < 5)
					counts[5]++;

				number++;
				ret.getWorkers().add(item);
			}
		}

		for (int index = 0; index < SumTypes.length; index++) {
			ret.getSums().add(LeaderSum.create(SumTypes[index], counts[index], number));
		}

		return ret;
	}

	@Override
	public FilterParam categories() {

		validateDataset();

		LeaderDataset db= LeaderCache.my().get();

		FilterParam ret = new FilterParam();
		ret.setJobs(db.getJobs());
		ret.setProfs(db.getProfs());
		ret.setTeams(db.getTeams());
		
		return ret;
	}

}
